package com.bumptech.glide.load.resource.bitmap;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.NonNull;

import com.bumptech.glide.load.EncodeStrategy;
import com.bumptech.glide.load.Options;
import com.bumptech.glide.load.ResourceEncoder;
import com.bumptech.glide.load.engine.Resource;
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool;

import java.io.File;

/**
 * Encodes {@link BitmapDrawable}s.
 * <p>
 * 对{@link BitmapDrawable}进行编码
 */
public class BitmapDrawableEncoder implements ResourceEncoder<BitmapDrawable> {

    private final BitmapPool bitmapPool;
    private final ResourceEncoder<Bitmap> encoder;

    public BitmapDrawableEncoder(BitmapPool bitmapPool, ResourceEncoder<Bitmap> encoder) {
        this.bitmapPool = bitmapPool;
        this.encoder = encoder;
    }

    @Override
    public boolean encode(@NonNull Resource<BitmapDrawable> data, @NonNull File file,
                          @NonNull Options options) {
        return encoder.encode(new BitmapResource(data.get().getBitmap(), bitmapPool), file, options);
    }

    @NonNull
    @Override
    public EncodeStrategy getEncodeStrategy(@NonNull Options options) {
        return encoder.getEncodeStrategy(options);
    }
}
